#include "mainwindow.h"
#include <QLabel>
#include <QStatusBar>
#include <QAction>
#include <QMenuBar>
#include <QToolBar>
#include <QApplication>
#include <QDesktopWidget>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMessageBox>
#include <QDebug>

//#include <QtSvg/QSvgWidget>
//#include <libssh/libssh.h>
#include <stdlib.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
 {
    setWindowTitle("*** OPA ***");
    setWindowIcon(QIcon("./images/asterisk.svg"));
    QDesktopWidget *desktop = QApplication::desktop();  // Устанавливаем начальный размер главного окна
    int width = desktop->width()/2;
    int height = desktop->height()/2;
    resize(width,height);

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();

    //scene = new QGraphicsScene;
    //view = new QGraphicsView;
    //view->setScene(scene);
    //view->show();



    LabelTab1 = new QLabel(" My example Tab1 ");
    LabelTab2 = new QLabel(" My example Tab2 ");
    LabelTab3 = new QLabel(" My example Tab3 ");

    // QWidget *centralWidget = new QWidget(this);
    QTabWidget *tabs = new QTabWidget;
    //tabs->addTab(new QWidget(),"TAB 1");
    tabs->addTab(LabelTab1,"TAB 1");
    tabs->addTab(LabelTab2,"TAB 2");
    tabs->addTab(LabelTab3,"TAB 3");

    // this->setCentralWidget(centralWidget);
    // this->setCentralWidget(view);

    frameMainWindows = new QFrame;
    frameLayout = new QVBoxLayout(frameMainWindows);
    frameLayout->addWidget(tabs);
    //frameLayout->addWidget(view);
    setCentralWidget(frameMainWindows);



 }

MainWindow::~MainWindow()
{
   // delete ui;
}

void MainWindow::createActions()
{
    //
    newActFile = new QAction(tr("&New"), this);
    newActFile->setShortcut(tr("Ctrl+N"));
    newActFile->setIcon(QIcon("./images/menu/1_files/document-new.svg"));
    newActFile->setStatusTip(tr("Create a new file"));
    // connect(newActFile, SIGNAL(triggered()), this, SLOT(newFile()));

    openActFile = new QAction(tr("&Open"), this);
    openActFile->setShortcut(tr("Ctrl+O"));
    openActFile->setIcon(QIcon("./images/menu/1_files/document-open.svg"));
    openActFile->setStatusTip(tr("Open an existing file"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    previousActFile = new QAction(tr("&Previos"), this);
    previousActFile->setShortcut(tr("Ctrl+P"));
    previousActFile->setIcon(QIcon("./images/menu/1_files/document-open-recent.svg"));
    previousActFile->setStatusTip(tr("Previous file"));
    //connect(previousActFile, SIGNAL(triggered()), this, SLOT(openFile()));

    wizardActFile = new QAction(tr("&Wizard"), this);
    wizardActFile->setShortcut(tr("Ctrl+W"));
    wizardActFile->setIcon(QIcon("./images/menu/1_files/wizard.svg"));
    wizardActFile->setStatusTip(tr("Wizard create desktop"));
    //connect(previousActFile, SIGNAL(triggered()), this, SLOT(openFile()));

    closeActFile = new QAction(tr("&Close"), this);
    closeActFile->setShortcut(tr("Ctrl+C"));
    closeActFile->setIcon(QIcon("./images/menu/1_files/document-close.svg"));
    closeActFile->setStatusTip(tr("Close file"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    saveActFile = new QAction(tr("&Save"), this);
    saveActFile->setShortcut(tr("Ctrl+S"));
    saveActFile->setIcon(QIcon("./images/menu/1_files/document-save.svg"));
    saveActFile->setStatusTip(tr("Save existing file"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    saveusActFile = new QAction(tr("Save&As"), this);
    saveusActFile->setShortcut(tr("Ctrl+U"));
    saveusActFile->setIcon(QIcon("./images/menu/1_files/document-save-as.svg"));
    saveusActFile->setStatusTip(tr("Save us file"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    printActFile = new QAction(tr("&Print"), this);
    printActFile->setShortcut(tr("Ctrl+P"));
    printActFile->setIcon(QIcon("./images/menu/1_files/document-print.svg"));
    printActFile->setStatusTip(tr("Print page"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    exitActFile = new QAction(tr("&Exit"), this);
    exitActFile->setShortcut(tr("Ctrl+E"));
    exitActFile->setIcon(QIcon("./images/menu/1_files/exit.svg"));
    exitActFile->setStatusTip(tr("Exit from programm"));
    connect(exitActFile, SIGNAL(triggered()), qApp, SLOT(quit()));      /*Выход из программы через трей*/

    editmodeActEdit = new QAction(tr("Edit &Mode"), this);
    editmodeActEdit->setShortcut(tr("Ctrl+M"));
    editmodeActEdit->setIcon(QIcon("./images/menu/2_edit/edit-mode.svg"));
    editmodeActEdit->setStatusTip(tr("Swich Edit Mode"));
    editmodeActEdit->setCheckable(true);
    //editmodeActEdit->setChecked(true);
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    addAgentActEdit = new QAction(tr("Add"), this);
    //addAgentActEdit->setShortcut(tr("Ctrl+P"));
    addAgentActEdit->setStatusTip(tr("Add agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    delAgentActEdit = new QAction(tr("Delete"), this);
    //delAgentActEdit->setShortcut(tr("Ctrl+P"));
    delAgentActEdit->setStatusTip(tr("Delete agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    upAgentActEdit = new QAction(tr("Up"), this);
    //upAgentActEdit->setShortcut(tr("Ctrl+P"));
    upAgentActEdit->setStatusTip(tr("Move up agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    downAgentActEdit = new QAction(tr("Down"), this);
    //downAgentActEdit->setShortcut(tr("Ctrl+P"));
    downAgentActEdit->setStatusTip(tr("Move down agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    leftAgentActEdit = new QAction(tr("Left"), this);
    //leftAgentActEdit->setShortcut(tr("Ctrl+P"));
    leftAgentActEdit->setStatusTip(tr("Move left agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    rightAgentActEdit = new QAction(tr("Right"), this);
    //rightAgentActEdit->setShortcut(tr("Ctrl+P"));
    rightAgentActEdit->setStatusTip(tr("Move right agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    fistAgentActEdit = new QAction(tr("Fist"), this);
    //fistAgentActEdit->setShortcut(tr("Ctrl+P"));
    fistAgentActEdit->setStatusTip(tr("Make fist agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    lastAgentActEdit = new QAction(tr("Last"), this);
    //lastAgentActEdit->setShortcut(tr("Ctrl+P"));
    lastAgentActEdit->setStatusTip(tr("Make last agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    increaseAgentActEdit = new QAction(tr("Increase"), this);
    //increaseAgentActEdit->setShortcut(tr("Ctrl+P"));
    increaseAgentActEdit->setStatusTip(tr("Increase windows agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    decreaseAgentActEdit = new QAction(tr("Decrease"), this);
    //decreaseAgentActEdit->setShortcut(tr("Ctrl+P"));
    decreaseAgentActEdit->setStatusTip(tr("Decrease windows agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    addAvatarActEdit = new QAction(tr("Add avatar"), this);
    //addAvatarActEdit->setShortcut(tr("Ctrl+P"));
    addAvatarActEdit->setStatusTip(tr("Add avatar to agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    editDataActEdit = new QAction(tr("Edit data"), this);
    //editDataActEdit->setShortcut(tr("Ctrl+P"));
    editDataActEdit->setStatusTip(tr("Edit data to agent"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    addBookActEdit = new QAction(tr("Add"), this);
    //addBookActEdit->setShortcut(tr("Ctrl+P"));
    addBookActEdit->setStatusTip(tr("Add bookmark"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    editBookActEdit = new QAction(tr("Edit"), this);
    //editBookActEdit->setShortcut(tr("Ctrl+P"));
    editBookActEdit->setStatusTip(tr("Edit bookmark"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    moveBookActEdit = new QAction(tr("Move"), this);
    //moveBookActEdit->setShortcut(tr("Ctrl+P"));
    moveBookActEdit->setStatusTip(tr("Move bookmark"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    delBookActEdit = new QAction(tr("Delete"), this);
    //delBookActEdit->setShortcut(tr("Ctrl+P"));
    delBookActEdit->setStatusTip(tr("Delete bookmark"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    addGroupActEdit = new QAction(tr("Add"), this);
    //addGroupActEdit->setShortcut(tr("Ctrl+P"));
    addGroupActEdit->setStatusTip(tr("Add group"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    editGroupActEdit = new QAction(tr("Edit"), this);
    //editGroupActEdit->setShortcut(tr("Ctrl+P"));
    editGroupActEdit->setStatusTip(tr("Edit group"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    moveGroupActEdit = new QAction(tr("Move"), this);
    //moveGroupActEdit->setShortcut(tr("Ctrl+P"));
    moveGroupActEdit->setStatusTip(tr("Move group"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    delGroupActEdit = new QAction(tr("Delete"), this);
    //delGroupActEdit->setShortcut(tr("Ctrl+P"));
    delGroupActEdit->setStatusTip(tr("Delete group"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    addColumnActEdit = new QAction(tr("Add"), this);
    //addColumnActEdit->setShortcut(tr("Ctrl+P"));
    addColumnActEdit->setStatusTip(tr("Add column"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    delColumnActEdit = new QAction(tr("Delete"), this);
    //delColumnActEdit->setShortcut(tr("Ctrl+P"));
    delColumnActEdit->setStatusTip(tr("Delete column"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));



    aboutQtActHelp = new QAction(tr("About Qt"), this);
    //aboutQtActHelp->setShortcut(tr("Ctrl+P"));
    aboutQtActHelp->setIcon(QIcon("./images/menu/7_help/Qt-logo.svg"));
    aboutQtActHelp->setStatusTip(tr("About Qt"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    licenseActHelp = new QAction(tr("License"), this);
    //licenseActHelp->setShortcut(tr("Ctrl+P"));
    licenseActHelp->setIcon(QIcon("./images/menu/7_help/Heckert_GNU_white.svg"));
    licenseActHelp->setStatusTip(tr("License"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    aboutProgActHelp = new QAction(tr("About programm"), this);                         // Диалог с информацией о Программе
    aboutProgActHelp->setIcon(QIcon("./images/menu/7_help/help-about"));
    aboutProgActHelp->setStatusTip(tr("About programm"));
    connect(aboutProgActHelp, SIGNAL(triggered()), this, SLOT(aboutProgSlot()));

    sendFeedActHelp = new QAction(tr("Send feedback"), this);
    //sendFeedActHelp->setShortcut(tr("Ctrl+P"));
    sendFeedActHelp->setStatusTip(tr("Send feedback"));
    //connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));





}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newActFile);
    fileMenu->addAction(openActFile);
    fileMenu->addAction(previousActFile);
    fileMenu->addSeparator();
    fileMenu->addAction(wizardActFile);
    fileMenu->addSeparator();
    fileMenu->addAction(closeActFile);
    fileMenu->addAction(saveActFile);
    fileMenu->addAction(saveusActFile);
    fileMenu->addSeparator();
    fileMenu->addAction(printActFile);
    fileMenu->addSeparator();
    fileMenu->addAction(exitActFile);
    fileMenu->addSeparator();
    // editMenu
    editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(editmodeActEdit);
    editMenu->addSeparator();
    agentEdit = new QMenu(tr("Agent"),editMenu);        //Подменю создания-удаления агента
    editMenu->addMenu(agentEdit);
    agentEdit->addAction(addAgentActEdit);
    agentEdit->addAction(delAgentActEdit);
    moveEdit  = new QMenu(tr("Agent move"),editMenu);   //Подменю перемещения агента
    editMenu->addMenu(moveEdit);
    moveEdit->addAction(upAgentActEdit);
    moveEdit->addAction(downAgentActEdit);
    moveEdit->addAction(leftAgentActEdit);
    moveEdit->addAction(rightAgentActEdit);
    moveEdit->addAction(fistAgentActEdit);
    moveEdit->addAction(lastAgentActEdit);
    editMenu->addSeparator();
    windowsEdit = new QMenu(tr("Windows agent"),editMenu); //Подменю изменения размера окна агента
    editMenu->addMenu(windowsEdit);
    windowsEdit->addAction(increaseAgentActEdit);
    windowsEdit->addAction(decreaseAgentActEdit);
    editMenu->addSeparator();
    editMenu->addAction(addAvatarActEdit);
    editMenu->addAction(editDataActEdit);
    editMenu->addSeparator();
    bookmarkEdit = new QMenu(tr("Bookmark"),editMenu); //Подменю обработки закладок
    editMenu->addMenu(bookmarkEdit);
    bookmarkEdit->addAction(addBookActEdit);
    bookmarkEdit->addAction(editBookActEdit);
    bookmarkEdit->addAction(moveBookActEdit);
    bookmarkEdit->addAction(delBookActEdit);
    editMenu->addSeparator();
    groupEdit = new QMenu(tr("Group"),editMenu);   //Подменю обработки групп
    editMenu->addMenu(groupEdit);
    groupEdit->addAction(addGroupActEdit);
    groupEdit->addAction(editGroupActEdit);
    groupEdit->addAction(moveGroupActEdit);
    groupEdit->addAction(delGroupActEdit);
    editMenu->addSeparator();
    columnEdit = new QMenu(tr("Column"),editMenu);       //Подменю обработки колонок
    editMenu->addMenu(columnEdit);
    columnEdit->addAction(addColumnActEdit);
    columnEdit->addAction(delColumnActEdit);
    // actionMenu
    actionMenu = menuBar()->addMenu(tr("&Action"));

    // tuningMenu
    tuningMenu = menuBar()->addMenu(tr("&Tuning"));
    // viewgMenu
    viewgMenu = menuBar()->addMenu(tr("&View"));

    reportMenu = menuBar()->addMenu(tr("&Report"));

    helpMenu = menuBar()->addMenu(tr("&Help"));
    //helpMenu->addAction(aboutQtActHelp);
    helpMenu->addAction(tr("About Qt"), qApp, SLOT(aboutQt()));
    helpMenu->addAction(licenseActHelp);
    helpMenu->addAction(aboutProgActHelp);
    helpMenu->addAction(sendFeedActHelp);


}

void MainWindow::createToolBars()
{
    serverStartBut =new QPushButton;
    serverStartBut->setIcon(QIcon("./images/menu/1_files/document-new.svg"));
    serverStartBut->setFixedSize(serverStartBut->iconSize());
    serverStartBut->setFlat(true);
    serverStartBut->setCheckable(true);

//    Здесь можно воспользоваться CSS-ом
//    QString css = "QPushButton { border: none;}";
//    QPushButton* button = new QPushButton(parent);
//    button->setStyleSheet(css);

    connect(serverStartBut, SIGNAL(released()), this, SLOT(releaseServerButton()));

    serverToolBar = new QToolBar;
    comboBoxToolBar = new QComboBox(serverToolBar);
    comboBoxToolBar->addItem("Asterisk *",1);
    comboBoxToolBar->addItem("Asterisk **",2);
    comboBoxToolBar->addItem("Asterisk ***",3);
    serverToolBar->addWidget(comboBoxToolBar);                                  // вставляем в панель инструментов кобо бокс для выбопа сервера
    serverToolBar->addWidget(serverStartBut);                                   // вставляем кнопку запуска
    //serverToolBar->setHidden(true);

    fileToolBar = new QToolBar(tr("File"), this);
    fileToolBar->addAction(newActFile);
    fileToolBar->addAction(openActFile);
    fileToolBar->addAction(wizardActFile);

    //fileToolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);

    //fileToolBar->setDisabled(true);
    //fileToolBar->setHidden(true);

    //addToolBar(Qt::TopToolBarArea, fileToolBar);

    addToolBar(serverToolBar);
    addToolBar(fileToolBar);


}
void MainWindow::releaseServerButton()
{
    if (serverStartBut->isChecked())
        {
        // qDebug() << serverStartBut->icon();
        serverStartBut->setIcon(QIcon("./images/menu/1_files/document-open.svg"));
        serverStartBut->setChecked(true);
        // serverStartBut->
        // qDebug() << serverStartBut->icon();
        }

    if ( !serverStartBut->isChecked())
        {
        serverStartBut->setIcon(QIcon("./images/menu/1_files/document-new.svg"));
        serverStartBut->setChecked(false);
        }


}




void MainWindow::createStatusBar()
{
    //
    // locationLabel = new QLabel(" My program is BEST ");
    //locationLabel->setAlignment(Qt::AlignHCenter);
    //locationLabel->setMinimumSize(locationLabel->sizeHint());
    // statusBar()->addWidget(locationLabel);
    statusBar()->showMessage(" My program is BEST !");

    //statusBar()->setLayout();
    //updateStatusBar();

}

void MainWindow::updateStatusBar()
{
    //locationLabel->setText(spreadsheet->currentLocation());
    //formulaLabel->setText(spreadsheet->currentFormula());
}

void MainWindow::aboutProgSlot()
{
 QMessageBox aboutMsgBox(this);
 aboutMsgBox.setWindowIcon(QIcon("./images/asterisk.svg"));
 aboutMsgBox.setWindowTitle(tr("About programm"));
 aboutMsgBox.setIcon(QMessageBox::Information);                         // Тип иконки сообщени
 aboutMsgBox.setText(tr("About programm!\n It's very cool programm!!!"));
 aboutMsgBox.setModal(true);
 aboutMsgBox.exec();
}
